package org.qb.mapreduce.outputFormat;

import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.RecordWriter;
import org.apache.hadoop.mapreduce.TaskAttemptContext;

import java.io.IOException;



public class LogRecordWriter extends RecordWriter<Text, NullWritable> {

    private final FSDataOutputStream atguiguOut;
    private final FSDataOutputStream otherOut;

    public LogRecordWriter(TaskAttemptContext taskAttemptContext) {
        //创建两条流
        try {
            FileSystem fs = FileSystem.get(taskAttemptContext.getConfiguration());

            //创建流
            atguiguOut = fs.create(new Path("D:\\big data\\hadoop3.X\\output\\outputFormat\\atguigu.log"));

            otherOut = fs.create(new Path("D:\\big data\\hadoop3.X\\output\\outputFormat\\other.log"));


        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void write(Text text, NullWritable nullWritable) throws IOException, InterruptedException {

        //具体写
        if (text.toString().contains("atguigu")){
            atguiguOut.writeBytes(text + "\n");
        }else otherOut.writeBytes(text + "\n");

    }

    @Override
    public void close(TaskAttemptContext taskAttemptContext) throws IOException, InterruptedException {


        //关流
        IOUtils.closeStream(atguiguOut);
        IOUtils.closeStream(otherOut);

    }
}
